﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using TS.Commons;
using TS.Entities;

namespace TS.DataAccess
{
    public class ConfigurationDataAccess
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        public static Configuration ParseToEntity(IDataRecord dataRecord)
        {
            return new Configuration
            {
                IsClosed = dataRecord["IsClosed"].ToStringOrDefault().ToNumeric(),
                ViewCount = dataRecord["ViewCount"].ToStringOrDefault().ToNumeric(),
                WebName = dataRecord["WebName"].ToStringOrDefault().ToStringOrDefault(),
                WebLink = dataRecord["WebLink"].ToStringOrDefault().ToStringOrDefault(),
                WebEmail = dataRecord["WebEmail"].ToStringOrDefault().ToStringOrDefault(),
                Contact = dataRecord["Contact"].ToStringOrDefault().ToStringOrDefault(),
                PageSize = dataRecord["PageSize"].ToStringOrDefault().ToNumeric(),
                Notify = dataRecord["Notify"].ToStringOrDefault().ToStringOrDefault(),
                ShortAddress = dataRecord["ShortAddress"].ToStringOrDefault().ToStringOrDefault(),
                Sologan = dataRecord["Sologan"].ToStringOrDefault().ToStringOrDefault(),
                WebTel = dataRecord["WebTel"].ToStringOrDefault().ToStringOrDefault(),
                Address = dataRecord["Address"].ToStringOrDefault().ToStringOrDefault(),
                ClosedComment = dataRecord["ClosedComment"].ToStringOrDefault().ToStringOrDefault()
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public int UpdateConfiguration(Configuration config)
        {
            int result = 0;
            if (config != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              //new SqlParameter("@IsClosed",config.IsClosed),
                                                              //new SqlParameter("@ViewCount",config.ViewCount),
                                                              new SqlParameter("@WebName",config.WebName),
                                                              new SqlParameter("@WebLink",config.WebLink),
                                                              new SqlParameter("@WebEmail",config.WebEmail),
                                                             // new SqlParameter("@Contact",config.Contact),
                                                              new SqlParameter("@PageSize",config.PageSize),
                                                              new SqlParameter("@Notify",config.Notify),
                                                              new SqlParameter("@WebTel",config.WebTel),
                                                              new SqlParameter("@Address",config.Address),
                                                              new SqlParameter("@ShortAddress",config.ShortAddress),
                                                              new SqlParameter("@Sologan",config.Sologan),
                                                             // new SqlParameter("@ClosedComment",config.ClosedComment)

                                                          };


                using (IDataReader reader = SqlHelper.ExecuteReader(UIConfig.UpdateConfig, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["ConfigId"], CultureInfo.CurrentCulture);

                        }
                        return result;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public int UpdateClose(Configuration config)
        {
            int result = 0;
            if (config != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@IsClosed",config.IsClosed),
                                                              new SqlParameter("@ClosedComment",config.ClosedComment)

                                                          };


                using (IDataReader reader = SqlHelper.ExecuteReader(UIConfig.UpdateClosed, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["ConfigId"], CultureInfo.CurrentCulture);

                        }
                        return result;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Configuration GetConfig()
        {
            var collection = new Collection<SqlParameter>();
            using (IDataReader iDataReader = SqlHelper.ExecuteReader(UIConfig.SelectConfig, collection))
            {
                if (iDataReader.Read())
                {
                    return ParseToEntity(iDataReader);
                }
            }
            return null;
        }

        public int UpdateViewCount()
        {
            int result = 0;
                using (IDataReader reader = SqlHelper.ExecuteReader(UIConfig.UpdateViewCount, null))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["ConfigId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            return result;
        }
    }
}
